
package com.idealighter.game.games.errenniuniu.handler;

import com.idealighter.game.core.annotation.ResMsgHandler;
import com.idealighter.game.dictionary.domain.ErrenniuniuRoomDomain;
import com.idealighter.game.gamehall.dto.MemInfo;
import com.idealighter.game.gamehall.dto.SeatInfo;
import com.idealighter.game.gamehall.dto.TableInfo;
import com.idealighter.game.games.errenniuniu.ErRenNiuNiuPlayer;
import com.idealighter.game.games.errenniuniu.message.ReqBetMsg;
import com.idealighter.game.games.errenniuniu.message.ReqCallDealerMsg;
import com.idealighter.game.games.errenniuniu.message.ResCallDealerMsg;
import com.idealighter.game.message.core.ResMessage;
import com.idealighter.game.robot.core.Player;
import com.idealighter.game.robot.handler.ResMessageHandler;
import com.idealighter.utils.code.RandCodeUtil;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;


@ResMsgHandler(502206)
public class ResCallDealerHandler implements ResMessageHandler {

  @Override
  public void action(Player player, ResMessage message) {
    ResCallDealerMsg msg = (ResCallDealerMsg) message;

    long playerId = player.playerId;
    if (msg.getDealer() == 0) { // 没有确定庄家,继续叫庄
      if (msg.getNextCallPlayer() == playerId) {
        // 随机1到3秒叫庄
        player.schedule(() -> callDealer(player, RandCodeUtil.randomBoolean()),
            RandCodeUtil.random(1, 3), TimeUnit.SECONDS);
      }
    } else if (msg.getDealer() != playerId) { // 确定庄家,闲家下注
      // 随机1到3秒下注
      player.schedule(() -> bet(player, RandCodeUtil.randomList(abledBetChips(player))),
          RandCodeUtil.random(1, 3), TimeUnit.SECONDS);
    }
  }

  /**
   * 叫庄.
   * 
   * @param player .
   * @param call .
   */
  public void callDealer(Player player, boolean call) {
    ReqCallDealerMsg msg = new ReqCallDealerMsg();
    msg.setCall((byte) (call ? 1 : 0));
    player.sendMsg(msg);
  }

  /**
   * 下注.
   * 
   * @param player .
   * @param chip .
   */
  public void bet(Player player, Long chip) {
    ReqBetMsg msg = new ReqBetMsg();
    msg.setChip(chip);
    player.sendMsg(msg);
  }

  /**
   * 该seat可用的下注的集合,用庄家和闲家中最小的一家来计算.
   * 
   * @param player .
   * @return .
   */
  private List<Long> abledBetChips(Player player) {
    TableInfo table = player.table(player.playerId);

    ErrenniuniuRoomDomain roomDom = ((ErRenNiuNiuPlayer) player).roomDomain;

    SeatInfo seat1 = table.getSeats().get(0);
    SeatInfo seat2 = table.getSeats().get(1);

    MemInfo member1 = player.member(seat1.getPlayerId());
    MemInfo member2 = player.member(seat2.getPlayerId());

    long minChips =
        member1.getChips() > member2.getChips() ? member2.getChips() : member1.getChips();

    // 最低下注
    long minOne = roomDom.getMinOne();
    long maxOne = roomDom.getMaxOne();

    if (minChips < minOne * 2 * 8) {
      return Arrays.asList(minOne);
    } else if (minChips < minOne * 3 * 8) {
      return Arrays.asList(minOne, minOne * 2);
    } else if (minChips < minOne * 4 * 8) {
      return Arrays.asList(minOne, minOne * 2, minOne * 3);
    } else if (minChips < maxOne * 4 * 8) {
      return Arrays.asList(minChips / 32, minChips / 16, minChips * 3 / 32, minChips / 8);
    } else {
      return Arrays.asList(maxOne, maxOne * 2, maxOne * 3, maxOne * 4);
    }
  }
}
